﻿using System.Net;
using System.Text;

namespace LanqiQAwebapi.ToolsHader
{
    public class AddMessage
    {

        public static int SendMessage(string yzphone)
        {
            try
            {
       /*         string content;
                string PostUrl = "https://106.ihuyi.com/webservice/sms.php?method=Submit";
                string account = "C85815575";//登录“互亿无线网站”查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
                string password = "6eebaff7bb2f1ca3b9ce50ff9b72b425"; //登录“互亿无线网站”查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
                string mobile = yzphone;
                //string mobile = Request.QueryString["mobile"];*/
                Random rad = new Random();
                int mobile_code = rad.Next(100000, 1000000);   //生成随机数                       
             /*   content = "您的验证码是：" + mobile_code + " 。请不要把验证码泄露给其他人。";
                string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";  //用户名+密码+注册的手机号+验证码
                UTF8Encoding encoding = new UTF8Encoding();  //万国码
                byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content)); //将字符串postStrTpl中的格式项替换为四个个指定的 Object 实例的值的文本等效项。再转为二进制数据



                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);//对统一资源标识符 (URI) 发出请求。 这是一个 abstract 类。
                myRequest.Method = "POST";

                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = postData.Length;

                Stream newStream = myRequest.GetRequestStream(); //
                                                                 // Send the data.
                newStream.Write(postData, 0, postData.Length);
                newStream.Flush();
                newStream.Close();

                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                if (myResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

                    //code状态返回值；msg查询结果描述
                    string res = reader.ReadToEnd();
                    int len1 = res.IndexOf("</code>");
                    int len2 = res.IndexOf("<code>");
                    string code = res.Substring((len2 + 6), (len1 - len2 - 6));

                    int len3 = res.IndexOf("</msg>");
                    int len4 = res.IndexOf("<msg>");
                    string msg = res.Substring((len4 + 5), (len3 - len4 - 5));

                }
                else
                {

                    //访问失败
                }*/
                return mobile_code;
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            return 0;
        }
    }
}
